 
#!/bin/bash

essid="zoom50"
channel=11
t=3         #unsuccessful attempts before assuming its at home

getmship () {
	ifconfig msh0|grep "t add"|sed 's/^[^:]*:\([^ ]*\) .*$/\1/'
}

assoc () {
for i in `seq 1 $t`
do
	iwconfig eth0 essid $essid channel $channel mode managed
	killall -9 dhclient &> /dev/null
	sleep 2	
	dhclient eth0 && ping -c 1 schoolserver &> /dev/null && echo OK && break
done	
}
sleep 30      #do not reduce this delay. It must start after sugar boot
tmp=`mktemp`
assoc > $tmp
! cat "$tmp"|grep "OK" > /dev/null && echo "AT HOME" && exit
echo "AT SCHOOL" 
service NetworkManager stop   #you may comment these 3 lines to test with NM on
sleep 5                       #
assoc > /dev/null             #

#The following is useful only when NM is kept alive
#The most clear signal that NM has affected our configuration is the msh0 IP address
#The first thing NM will do is setup the msh0 interface
#The following lines detect changes in msh0, and if so, it reassociates
while true
do
if [ -n "$getmship" ] 
	then ! echo "`assoc`"|grep "OK" > /dev/null && echo "AT HOME" && exit
fi 
sleep 1
done


